<?php
session_start();
require_once 'config/database.php';
require_once 'includes/functions.php';

$success_message = '';
$error_message = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name = sanitizeInput($_POST['name']);
    $email = sanitizeInput($_POST['email']);
    $subject = sanitizeInput($_POST['subject']);
    $message = sanitizeInput($_POST['message']);
    
    if (empty($name) || empty($email) || empty($subject) || empty($message)) {
        $error_message = 'Please fill in all fields.';
    } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
        $error_message = 'Please enter a valid email address.';
    } else {
        try {
            $stmt = $pdo->prepare("INSERT INTO contact_messages (name, email, subject, message) VALUES (?, ?, ?, ?)");
            $saved = $stmt->execute([$name, $email, $subject, $message]);
            
            if ($saved) {
                $success_message = 'Thank you for your message! We will get back to you within 24 hours.';
                $_POST = array();
            } else {
                $error_message = 'Failed to save your message. Please try again.';
            }
        } catch (PDOException $e) {
            $error_message = 'Database error: ' . $e->getMessage();
        }
    }
}

$page_title = "Contact Us";
?>
<?php include 'includes/header.php'; ?>

<main style="padding: 2rem 0;">
    <div class="container">
        <!-- Hero Section -->
        <section class="hero" style="background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); margin-bottom: 4rem;">
            <div class="container">
                <h1>Get in Touch</h1>
                <p>We'd love to hear from you. Send us a message and we'll respond as soon as possible.</p>
            </div>
        </section>

        <div style="display: grid; grid-template-columns: 1fr 1fr; gap: 4rem;">
            <!-- Contact Form -->
            <div>
                <div class="card">
                    <div class="card-header">
                        <h2>Send us a Message</h2>
                    </div>
                    
                    <?php if ($success_message): ?>
                        <div class="alert alert-success"><?php echo $success_message; ?></div>
                    <?php endif; ?>
                    
                    <?php if ($error_message): ?>
                        <div class="alert alert-error"><?php echo $error_message; ?></div>
                    <?php endif; ?>
                    
                    <form method="POST" action="">
                        <div class="form-group">
                            <label for="name">Full Name *</label>
                            <input type="text" id="name" name="name" required value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ''; ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="email">Email Address *</label>
                            <input type="email" id="email" name="email" required value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email']) : ''; ?>">
                        </div>
                        
                        <div class="form-group">
                            <label for="subject">Subject *</label>
                            <select id="subject" name="subject" required>
                                <option value="">Select a subject</option>
                                <option value="general" <?php echo (isset($_POST['subject']) && $_POST['subject'] === 'general') ? 'selected' : ''; ?>>General Inquiry</option>
                                <option value="order" <?php echo (isset($_POST['subject']) && $_POST['subject'] === 'order') ? 'selected' : ''; ?>>Order Support</option>
                                <option value="product" <?php echo (isset($_POST['subject']) && $_POST['subject'] === 'product') ? 'selected' : ''; ?>>Product Question</option>
                                <option value="shipping" <?php echo (isset($_POST['subject']) && $_POST['subject'] === 'shipping') ? 'selected' : ''; ?>>Shipping & Delivery</option>
                                <option value="returns" <?php echo (isset($_POST['subject']) && $_POST['subject'] === 'returns') ? 'selected' : ''; ?>>Returns & Exchanges</option>
                                <option value="wholesale" <?php echo (isset($_POST['subject']) && $_POST['subject'] === 'wholesale') ? 'selected' : ''; ?>>Wholesale Inquiry</option>
                                <option value="other" <?php echo (isset($_POST['subject']) && $_POST['subject'] === 'other') ? 'selected' : ''; ?>>Other</option>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label for="message">Message *</label>
                            <textarea id="message" name="message" rows="6" required placeholder="Please describe your inquiry in detail..."><?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message']) : ''; ?></textarea>
                        </div>
                        
                        <div class="form-group">
                            <button type="submit" class="btn btn-primary" style="width: 100%;">
                                <i class="fas fa-paper-plane"></i> Send Message
                            </button>
                        </div>
                    </form>
                </div>
            </div>

            <!-- Contact Information -->
            <div>
                <div class="card">
                    <div class="card-header">
                        <h2>Contact Information</h2>
                    </div>
                    
                    <div style="display: flex; flex-direction: column; gap: 2rem;">
                        <div style="display: flex; align-items: center; gap: 1rem;">
                            <div style="background: #e74c3c; color: white; width: 50px; height: 50px; border-radius: 50%; display: flex; align-items: center; justify-content: center;">
                                <i class="fas fa-map-marker-alt"></i>
                            </div>
                            <div>
                                <h3 style="margin: 0; color: #2c3e50;">Address</h3>
                                <p style="margin: 0; color: #7f8c8d;">
                                    123 Fragrance Lane<br>
                                    Luxury District, NY 10001<br>
                                    United States
                                </p>
                            </div>
                        </div>
                        
                        <div style="display: flex; align-items: center; gap: 1rem;">
                            <div style="background: #e74c3c; color: white; width: 50px; height: 50px; border-radius: 50%; display: flex; align-items: center; justify-content: center;">
                                <i class="fas fa-phone"></i>
                            </div>
                            <div>
                                <h3 style="margin: 0; color: #2c3e50;">Phone</h3>
                                <p style="margin: 0; color: #7f8c8d;">
                                    +1 (555) 123-4567<br>
                                    Mon-Fri: 9AM-6PM EST
                                </p>
                            </div>
                        </div>
                        
                        <div style="display: flex; align-items: center; gap: 1rem;">
                            <div style="background: #e74c3c; color: white; width: 50px; height: 50px; border-radius: 50%; display: flex; align-items: center; justify-content: center;">
                                <i class="fas fa-envelope"></i>
                            </div>
                            <div>
                                <h3 style="margin: 0; color: #2c3e50;">Email</h3>
                                <p style="margin: 0; color: #7f8c8d;">
                                    hello@roielune.com<br>
                                    support@roielune.com
                                </p>
                            </div>
                        </div>
                        
                        <div style="display: flex; align-items: center; gap: 1rem;">
                            <div style="background: #e74c3c; color: white; width: 50px; height: 50px; border-radius: 50%; display: flex; align-items: center; justify-content: center;">
                                <i class="fas fa-clock"></i>
                            </div>
                            <div>
                                <h3 style="margin: 0; color: #2c3e50;">Business Hours</h3>
                                <p style="margin: 0; color: #7f8c8d;">
                                    Monday - Friday: 9:00 AM - 6:00 PM<br>
                                    Saturday: 10:00 AM - 4:00 PM<br>
                                    Sunday: Closed
                                </p>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- FAQ Section -->
                <div class="card" style="margin-top: 2rem;">
                    <div class="card-header">
                        <h2>Frequently Asked Questions</h2>
                    </div>
                    
                    <div style="display: flex; flex-direction: column; gap: 1rem;">
                        <div>
                            <h4 style="color: #2c3e50; margin-bottom: 0.5rem;">How long does shipping take?</h4>
                            <p style="color: #7f8c8d; margin: 0; font-size: 0.9rem;">We offer free shipping on orders over $100. Standard shipping takes 3-5 business days, while express shipping takes 1-2 business days.</p>
                        </div>
                        
                        <div>
                            <h4 style="color: #2c3e50; margin-bottom: 0.5rem;">Can I return products?</h4>
                            <p style="color: #7f8c8d; margin: 0; font-size: 0.9rem;">Yes! We offer a 30-day return policy for all unused products in their original packaging.</p>
                        </div>
                        
                        <div>
                            <h4 style="color: #2c3e50; margin-bottom: 0.5rem;">Are your products eco-friendly?</h4>
                            <p style="color: #7f8c8d; margin: 0; font-size: 0.9rem;">Absolutely! We use sustainable ingredients and recyclable packaging to minimize our environmental impact.</p>
                        </div>
                        
                        <div>
                            <h4 style="color: #2c3e50; margin-bottom: 0.5rem;">Do you offer wholesale pricing?</h4>
                            <p style="color: #7f8c8d; margin: 0; font-size: 0.9rem;">Yes, we offer wholesale pricing for retailers and businesses. Please contact us for more information.</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</main>

<?php include 'includes/footer.php'; ?>
